package com.itboye.eweilisten.me.timu.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.text.InputType;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

import com.itboye.eweilisten.R;
import com.itboye.eweilisten.model.TIbeanTwo;
import com.itboye.eweilisten.util.widget.AutoResizeTextView;
import com.itboye.eweilisten.util.widget.FlowLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * 表格
 */
public class BiaoGeAdapter extends RecyclerView.Adapter<BiaoGeAdapter.MyViewHolder> {
    Context context;
    private List<TIbeanTwo> beans;
    private int totalEdit;
    int position = -1;

    public BiaoGeAdapter(Context context, List<TIbeanTwo> beans) {
        this.context = context;
        this.beans = beans;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_biaogge, null, false);
        MyViewHolder holder1 = new MyViewHolder(view);
        return holder1;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.timu_num.setText(position + 1 + "");

        ArrayList<ArrayList<String>> arrrayOut = beans.get(position).getContent();
        for (int i = 0; i < arrrayOut.size(); i++) {
            questionContentAdd(holder.tableLayout, arrrayOut, holder.answerLine, i);
        }

        for (int j = 0; j < beans.get(position).getAnswers().size(); j++) {
            answerLineContentAdd(holder.answerLine, beans.get(position).getOption().get(j).getOptions(), holder.tableLayout, position);
        }

    }

    /**
     * 内容
     */
    private void questionContentAdd(final TableLayout tableLayout, ArrayList<ArrayList<String>> arrrayOut, final FlowLayout answerLine, int i) {
        // 设置所有的item都可伸缩扩展
        tableLayout.setStretchAllColumns(true);

        TableRow.LayoutParams lp = new TableRow.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        //生成10行，8列的表格
        totalEdit = 0;

        TableRow tableRow = new TableRow(context);

        for (int j = 0; j < arrrayOut.get(i).size(); j++) {
            if ("_".equals(arrrayOut.get(i).get(j))) {
                AutoResizeTextView tv = new AutoResizeTextView(context);
                tv.setInputType(InputType.TYPE_NULL);
                tv.setGravity(Gravity.CENTER);
                tv.setBackgroundResource(R.drawable.table_frame_gray);
                tv.setTag(totalEdit);
                tv.setPadding(0, 20, 0, 20);
                totalEdit++;
                tv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        TextView editText = (TextView) v;
                        String ed = editText.getText().toString();
                        for (int i = 0; i < answerLine.getChildCount(); i++) {
                            TextView button = (TextView) answerLine.getChildAt(i);
                            String btn_text = button.getText().toString();
                            if (btn_text.equals(ed)) {
                                //  answerLine.getChildAt(i).setBackgroundColor(Color.parseColor("#0000ff"));
                                answerLine.getChildAt(i).setBackgroundDrawable(context.getResources().getDrawable(R.drawable.flag_01));
                                answerLine.getChildAt(i).setSelected(false);
                                break;
                            }
                        }
                        ((TextView) v).setText("");
                        for (int i = 0; i < tableLayout.getChildCount(); i++) {
                            TableRow tableRow = (TableRow) tableLayout.getChildAt(i);
                            for (int i1 = 0; i1 < tableRow.getChildCount(); i1++) {
                                if (tableRow.getChildAt(i1) instanceof AutoResizeTextView) {
                                    tableRow.getChildAt(i1).setBackgroundResource(R.drawable.table_frame_gray);
                                    tableRow.getChildAt(i1).setSelected(false);
                                }
                            }
                        }
                        v.setSelected(true);
                        v.setBackgroundColor(Color.parseColor("#3b7830"));
                    }
                });
                tv.setText("");
                tableRow.addView(tv);
            } else {
                TextView tv = new TextView(context);
                tv.setText(arrrayOut.get(i).get(j));
                tv.setPadding(0, 20, 0, 20);
                tv.setBackgroundResource(R.drawable.table_frame_gray);
                tv.setGravity(Gravity.CENTER);
                tableRow.addView(tv);
            }
        }
        //新建的TableRow添加到TableLayout
        tableLayout.addView(tableRow, lp);

    }

    /**
     * 答案
     */
    private void answerLineContentAdd(FlowLayout answerLine, String string, final TableLayout tableLayout,final int p) {
        // 设置添加的textvIew的参数
        RecyclerView.LayoutParams params = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        params.setMargins(0, 5, 25, 5);

        TextView textView = new TextView(context);
        textView.setText(string);
        textView.setTextSize(16);
        textView.setTextColor(Color.parseColor("#ffffff"));

        textView.setLayoutParams(params);
        textView.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.flag_01));
        answerLine.addView(textView);

        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //先判断有没有选中的编辑框
                boolean hasSelect = false;
                for (int i = 0; i < tableLayout.getChildCount(); i++) {
                    System.out.println(tableLayout.getChildAt(i) + ">>>外层");
                    TableRow tableRow = (TableRow) tableLayout.getChildAt(i);
                    for (int i1 = 0; i1 < tableRow.getChildCount(); i1++) {
                        if (tableRow.getChildAt(i1) instanceof AutoResizeTextView && tableRow.getChildAt(i1).isSelected()) {
                            hasSelect = true;
                            break;
                        }
                    }
                }
                if (!hasSelect) {
                    return;
                }
                if (v.isSelected()) {

                } else {
                    ((TextView) v).setBackgroundDrawable(context.getResources().getDrawable(R.drawable.flag_04));
                    v.setSelected(true);
                    for (int i = 0; i < tableLayout.getChildCount(); i++) {
                        TableRow tableRow = (TableRow) tableLayout.getChildAt(i);
                        for (int i1 = 0; i1 < tableRow.getChildCount(); i1++) {
                            if (tableRow.getChildAt(i1) instanceof AutoResizeTextView && tableRow.getChildAt(i1).isSelected()) {
                                position = Integer.parseInt(tableRow.getChildAt(i1).getTag() + "");

                                ((AutoResizeTextView) tableRow.getChildAt(i1)).setText(((TextView) v).getText().toString());
                                tableRow.getChildAt(i1).setBackgroundResource(R.drawable.table_frame_gray);
                                tableRow.getChildAt(i1).setSelected(false);
                            }
                        }
                    }
                    int totalPeroid = 2;
                    boolean hasFind = false;
                    while (totalPeroid >= 0) {
                        totalPeroid--;
                        position++;
                        if (position > totalEdit - 1) {
                            position = 0;
                        }
                        outer:
                        for (int i = 0; i < tableLayout.getChildCount(); i++) {
                            TableRow tableRow = (TableRow) tableLayout.getChildAt(i);

                            for (int i1 = 0; i1 < tableRow.getChildCount(); i1++) {
                                if (tableRow.getChildAt(i1) instanceof AutoResizeTextView) {
                                    AutoResizeTextView ed = (AutoResizeTextView) tableRow.getChildAt(i1);
                                    if (Integer.valueOf(ed.getTag() + "") == position && ed.getText().toString().equals("")) {
                                        tableRow.getChildAt(i1).setBackgroundColor(Color.parseColor("#3b7830"));
                                        tableRow.getChildAt(i1).setSelected(true);
                                        totalPeroid = -1;
                                        hasFind = true;
                                        break outer;
                                    } else {
                                        hasFind = false;
                                    }
                                }
                            }
                        }
                    }
                }

                //点击的时候遍历出答案是否正确
                StringBuffer sb = new StringBuffer();
                for (int i = 0; i < tableLayout.getChildCount(); i++) {
                    TableRow tableRow = (TableRow) tableLayout.getChildAt(i);
                    for (int i1 = 0; i1 < tableRow.getChildCount(); i1++) {
                        if (tableRow.getChildAt(i1) instanceof AutoResizeTextView) {
                            String to = ((TextView) tableRow.getChildAt(i1)).getText().toString();
                            sb.append(to);
                        }
                    }
                }
                StringBuilder ssss = new StringBuilder();
                for (int j = 0; j < beans.get(p).getAnwser_true().size(); j++) {
                    ssss.append(beans.get(p).getAnwser_true().get(j));
                }
                System.out.println("UIIHIUHHOIS" + sb);
                System.out.println("UIIHIUHHOIS" + ssss);
                if (sb.equals("")) {
                    beans.get(p).setXuan(2);//2代表没有输入
                } else if (sb.toString().equals(ssss.toString())) {
                    beans.get(p).setXuan(0);//0代表选择正确
                } else {
                    beans.get(p).setXuan(1);//0代表选择错误
                }

            }
        });

    }

    @Override
    public int getItemCount() {
        return beans.size();
    }

    // 自定义的ViewHolder，持有每个Item的的所有界面元素
    public class MyViewHolder extends RecyclerView.ViewHolder {
        public TextView timu_num;
        public TableLayout tableLayout;
        public FlowLayout answerLine;

        public MyViewHolder(View itemView) {
            super(itemView);
            timu_num = (TextView) itemView.findViewById(R.id.timu_num);
            tableLayout = (TableLayout) itemView.findViewById(R.id.tableLayout);
            answerLine = (FlowLayout) itemView.findViewById(R.id.answerLine);
        }
    }
}
